package com.wh.wisdomsite.xbox.us.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hikvision.cms.api.common.util.Digests;
import com.hikvision.cms.api.common.util.HttpClientSSLUtils;
import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.common.reponse.CommonResult;
import com.wh.wisdomsite.xbox.common.util.XboxUtils;
import com.wh.wisdomsite.xbox.pojo.us.GetDefaultUserUuidIn;
import com.wh.wisdomsite.xbox.pojo.us.GetDefaultUserUuidOut;
import com.wh.wisdomsite.xbox.pojo.us.GetUsersIn;
import com.wh.wisdomsite.xbox.pojo.us.GetUsersOut;
import com.wh.wisdomsite.xbox.us.dao.HkUserMapper;
import com.wh.wisdomsite.xbox.us.entity.HkUser;
import com.wh.wisdomsite.xbox.us.service.IHkUserService;
import com.wh.wisdomsite.xbox.utils.Const;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.us.service.impl
 * @date 2018年09月04日 11:18
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Service("hkUserService")
@Transactional(rollbackFor = {Exception.class,RuntimeException.class})
public class HkUserServiceImpl extends ServiceImpl<HkUserMapper, HkUser> implements IHkUserService {

    private static final Log logger = LogFactory.getLog(HkUserServiceImpl.class);

    @Autowired
    private HkUserMapper hkUserMapper;

    @Override
    public CommonResult getDefaultUserUuid() {
        CommonResult commonResult = new CommonResult();
        try{
            logger.info("获取海康默认用户->>getDefaultUserUuid->>开始");
            //获取URL
            String url = Const.HKWS_TR_OPENAPI_HTTP_URL + Const.HKWS_TR_INTERFACE_OPENAPI_SERVICE_BASE_GETDEFAULTUUID;
            GetDefaultUserUuidIn getDefaultUserUuidIn  = new GetDefaultUserUuidIn();
            //设置时间
            getDefaultUserUuidIn.setTime(System.currentTimeMillis());
            //生成参数模型
            String params = JSON.toJSONString(getDefaultUserUuidIn);
            //获取result
            String result = HttpClientSSLUtils.doPost(url + "?token=" + Digests.buildToken(url + "?" + params, null, Const.HKWS_TR_OPENAPI_SECRET), params);
            //获取响应结果
            commonResult = XboxUtils.getOpenAPICommonResult(commonResult, result, GetDefaultUserUuidOut.class);
            Const.HKWS_TR_DEFAULT_UUID = commonResult.getResult() + "";
            logger.info("获取默认用户->>getDefaultUserUuid->>结束");
        }catch (Exception e){
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            e.printStackTrace();
            logger.error("获取默认用户->>getDefaultUserUuid->>Exception " + e.getMessage());
        }
            return  commonResult;
    }

    @Override
    public CommonResult getUsers() {
        CommonResult commonResult = new CommonResult();
        try{
            logger.info("获取海康用户->>getUsers->>开始");
            //获取URL
            String url = Const.HKWS_TR_OPENAPI_HTTP_URL + Const.HKWS_TR_INTERFACE_OPENAPI_SERVICE_BASE_GETUSERS;
            GetUsersIn getUsersIn = new GetUsersIn();
            //设置时间
            getUsersIn.setTime(System.currentTimeMillis());
            //设置应用Key
            getUsersIn.setAppkey(Const.HKWS_TR_OPENAPI_APPKEY);
            //设置当前页
            getUsersIn.setPageNo(1);
            //设置每页条目
            getUsersIn.setPageSize(400);
            //设置默认用户Id
            getUsersIn.setOpUserUuid(Const.HKWS_TR_DEFAULT_UUID);
            //生成参数模型
            String params = JSON.toJSONString(getUsersIn);
             //获取result
            String result = HttpClientSSLUtils.doPost(url + "?token=" + Digests.buildToken(url + "?" + params, null, Const.HKWS_TR_OPENAPI_SECRET), params);
            //获取响应结果
            commonResult = XboxUtils.getOpenAPICommonResult(commonResult, result, GetUsersOut.class);
            logger.info("获取海康用户->>getUsers->>结束");
        }catch (Exception e){
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            e.printStackTrace();
            logger.error("获取海康用户->>getUsers->>Exception " + e.getMessage());
        }
            return  commonResult;
    }

    @Override
    public List<HkUser> getNotAllotUser() {
        return hkUserMapper.getNotAllotUser();
    }
}
